﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using CMS2.Models;

namespace CMS2.Controllers
{ 
    [Authorize(Roles="Administrator")]
    public class LidController : Controller
    {
        private CMSContext db = new CMSContext();

        //
        // GET: /Lid/

        public ViewResult Index()
        {
            return View(db.lid.ToList());
        }

        //
        // GET: /Lid/Details/5

        public ViewResult Details(int id)
        {
            LidModel lidmodel = db.lid.Find(id);
            return View(lidmodel);
        }

        //
        // GET: /Lid/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Lid/Create

        [HttpPost]
        public ActionResult Create(LidModel lidmodel)
        {
            if (ModelState.IsValid)
            {
                if (db.lid.Count() > 0)
                {
                    try
                    {
                        LidModel check = db.lid.SingleOrDefault(l => l.RollNo == lidmodel.RollNo);
                        if (check != null)
                        {
                            ViewBag.error = "Already exist";
                            return View("Create");
                    
                        }

                    }
                    catch (Exception e)
                    {
                        ViewBag.error = e.Message;
                        return View("Create");
                    }
                }

                db.lid.Add(lidmodel);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(lidmodel);
        }
        
        //
        // GET: /Lid/Edit/5
 
        public ActionResult Edit(int id)
        {
            LidModel lidmodel = db.lid.Find(id);
            return View(lidmodel);
        }

        //
        // POST: /Lid/Edit/5

        [HttpPost]
        public ActionResult Edit(LidModel lidmodel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(lidmodel).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(lidmodel);
        }

        //
        // GET: /Lid/Delete/5
 
        public ActionResult Delete(int id)
        {
            LidModel lidmodel = db.lid.Find(id);
            return View(lidmodel);
        }

        //
        // POST: /Lid/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            LidModel lidmodel = db.lid.Find(id);
            db.lid.Remove(lidmodel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult chawal()
        {
            ViewBag.chawal = (from u in db.Users
                             join l in db.lid on u.UserName equals l.Username
                             select new { roll = l.RollNo, userName = u.UserName, password = u.Password }).First();
            var o = ViewBag.chawal;
            int a = o.roll;
                             
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}